package com.bolt.report.util;

import com.bolt.report.definition.PaperSize;
import com.bolt.report.exception.ReportException;

import java.math.BigDecimal;

public class ToolUtil {

    private static final short TWIPS_PER_PIEXL = 15;


    private static float dpi = 96f;

    public static float mmToPixel(float mm) {
        float value = ((dpi * mm) / 25.4f);
        return value;
    }

    public static float pixelToPoint(float pixel) {
        float value = (pixel * 72 / dpi);
        return value ;
    }

    public static float pointToMM(float pt) {
        float value = ((pt / 72.0F) * 25.4f);
        return value;
    }


    // 像素转poi宽度
    public static float pixelToPoiWidth(float pixel) {
        float numChars = pixel * 15 * 2.5f;
        return numChars;
    }


    public static float pointToPixel(float point) {
        float value = point * 1.33f;
        return value;
    }


    public static   float  mmToPoint(float mm) {
        float value = inchesToPoints(mmToInches(mm));
        return value;
    }

    public static final float mmToInches(float value) {
        return value / 25.4f;
    }

    public static final float inchesToPoints(float value) {
        return value * 72.0F;
    }

    public static final float pointToinches(float value) {
        return value / 72.0F;
    }

    public static float millimetersConvertUnit(float value, PaperSize.SizeUnit unit) {
        if (unit == PaperSize.SizeUnit.MM) {
            return value;
        } else if (unit == PaperSize.SizeUnit.PX) {
            return ToolUtil.mmToPixel(value);
        } else if (unit == PaperSize.SizeUnit.PT) {
            return ToolUtil.mmToPoint(value);
        } else {
            return value;
        }
    }

    public static double pixelConvertUnit(float value, PaperSize.SizeUnit unit) {
        if (unit == PaperSize.SizeUnit.MM) {
            return pointToMM(pixelToPoint(value));
        } else if (unit == PaperSize.SizeUnit.PX) {
            return value;
        } else if (unit == PaperSize.SizeUnit.PT) {
            return pixelToPoint(value);
        } else {
            return value;
        }
    }


    public static void setDpi(float dpi) {
        ToolUtil.dpi = dpi;
    }

    public static int pointToTwip(int point) {
        return point * 20;
    }

    public static BigDecimal toBigDecimal(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        } else if (obj instanceof String) {
            if (obj.toString().trim().equals("")) {
                return new BigDecimal(0);
            }
            try {
                String str = obj.toString().trim();
                return new BigDecimal(str);
            } catch (Exception ex) {
                throw new ReportException("Can not convert " + obj + " to BigDecimal.");
            }
        } else if (obj instanceof Number) {
            Number n = (Number) obj;
            return new BigDecimal(n.doubleValue());
        }
        throw new ReportException("Can not convert " + obj + " to BigDecimal.");
    }


}